<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "cart".
 *
 * @property string $cartId
 * @property string $productId
 * @property integer $productnum
 * @property string $price
 * @property string $userId
 * @property string $createTime
 *
 * @property Product $product
 */
class Cart extends superModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cart';
    }
    
    public static function getCTableName()
    {
        return 'cart';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['productId', 'productnum', 'userId'], 'integer'],
            [['price'], 'number'],
//            [['createTime'], 'required'],
            [['createTime'], 'safe'],
//            [['productId'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['productId' => 'productId']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cartId' => 'ID',
            'productId' => '商品',
            'productnum' => '数量',
            'price' => '单价',
            'userId' => '用户',
            'createTime' => '创建时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProduct()
    {
        return $this->hasOne(Product::className(), ['productId' => 'productId']);
    }
 
        public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            $this->createTime = date("Y-m-d h-i-s", time());
            return TRUE;
        }
        return FALSE;
    }
}
